package com.thghh.fsbrowser.javafx;

import java.io.File;
import java.net.URL;
import java.util.List;

import javax.swing.text.html.parser.ContentModel;

import com.thghh.fsbrowser.javafx.util.LoadFxml;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * com.thghh.fsbrowser.javafx.MainApp
 * 
 * @author pengzhikang
 * @date 2018年9月20日下午11:02:52
 * @since 1.0
 */
public class MainApp extends Application {

	public static final String FILE_URL_PROPERTY = "fileUrl";	
	private static ContentModel contentModel;
	private SessionManager sessionManager;

	public static ContentModel getContentModel() {
		return contentModel;
	}

	@Override
	public void start(Stage stage) throws Exception {
		sessionManager = SessionManager.createSessionManager("MainApp");
		sessionManager.loadSession();

		List<String> args = getParameters().getRaw();
		if (!args.isEmpty()) {
			sessionManager.getProperties().setProperty(FILE_URL_PROPERTY,
					new File(args.get(0)).toURI().toURL().toString());
		}
		contentModel = new ContentModel();

		FXMLLoader mainLoader = new FXMLLoader();
		mainLoader.setLocation(LoadFxml.getResource(getClass(), "fxml/Main.fxml"));
		mainLoader.setBuilderFactory(new JavaFXBuilderFactory());
		Parent mainParent = mainLoader.load();
		MainController mainController = mainLoader.getController();

		URL url = LoadFxml.getResource(getClass(), "fxml/FileSystem.fxml");

		FXMLLoader leftLoader = new FXMLLoader();
		leftLoader.setBuilderFactory(new JavaFXBuilderFactory());
		leftLoader.setLocation(url);
		Parent leftParent = leftLoader.load();
		//FileSystemController leftConstroller = leftLoader.getController();
		//leftConstroller.getFileTab().setText("local");

		FXMLLoader rightLoader = new FXMLLoader();
		rightLoader.setBuilderFactory(new JavaFXBuilderFactory());
		rightLoader.setLocation(url);
		Parent rightParent = rightLoader.load();
		//FileSystemController rightConstroller = rightLoader.getController();
		//rightConstroller.getFileTab().setText("local");

		final StackPane leftFS = new StackPane();
		leftFS.getChildren().add(leftParent);

		final StackPane rightFS = new StackPane();
		rightFS.getChildren().add(rightParent);

		mainController.getFileSplitPane().getItems().addAll(leftFS, rightFS);

		Scene scene = new Scene(mainParent);
		stage.setScene(scene);
		stage.show();

		stage.setOnCloseRequest(event -> sessionManager.saveSession());

		// org.scenicview.ScenicView.show(contentModel.getSubScene().getRoot());
	}

	public static void main(String[] args) {
		launch(args);
	}

}
